// pages/mygroup/mygroup.js
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
var app = getApp()
var range3;
var sceneres = {}
var winScale = 750 / app.data.deviceInfo.windowWidth
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 20,
    currentTab: 0,
    all_group_list: [],
    listflag: true,
    has_more: true,
    showLoading: true,
    dialogflag: true,
    res_navbar: ['全部', '待付款', '待成团', '拼团成功', '拼团失败'],
    reminder: [{
      'tit': '您还没有拼团商品哦',
      'word': '拼团可得更多优惠~'
    }, {
      'tit': '您还没有拼团商品哦',
      'word': '拼团可得更多优惠~'
    }, {
      'tit': '您还没有拼团商品哦',
      'word': '拼团可得更多优惠~'
    }, {
      'tit': '您还没有拼团商品哦',
      'word': '拼团可得更多优惠~'
    }, {
      'tit': '您还没有拼团商品哦',
      'word': '拼团可得更多优惠~'
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    var that = this
    range3 = 1160 / winScale;
    var animation3 = wx.createAnimation({
      duration: 350,
      timingFunction: 'ease-in-out',
    });
    this.animation3 = animation3;
    this.setData({
      currentTab: options.groupindex || 0
    })
    if (options.floor) {
      this.setData({
        floor: 1
      })
    } else {
      this.setData({
        floor: 0
      })
    }
    this.changegrouplist(this.data.currentTab)
    console.log(sceneres)
    app.wechatSwitch().then(function(wechat_switch) { //是否打开微信授权开关
      app.commonLogin(sceneres, wechat_switch)
    })
  },
  onResNavbarTap: function(e) {
    this.setData({
      page: 1,
      page_size: 20,
      all_group_list: [],
      showLoading: true,
      currentTab: e.currentTarget.dataset.index
    })
    this.changegrouplist(e.currentTarget.dataset.index)
  },
  refreshgrouplist: function(a) {
    var that = this
    shihan.sendRequest(a, 'POST', {
        page: that.data.page,
        page_size: that.data.page_size
      }, {})
      .then(function(response) {
        if (response.data.code == 0 && response.data.data.sh_data.length > 0) {
          that.setData({
            page: that.data.page + 1,
            all_group_list: that.data.all_group_list.concat(response.data.data.sh_data),
            listflag: true,
            has_more: true
          })
          if (that.data.all_group_list.length == response.data.data.sh_total_rows) {
            that.setData({
              listflag: false,
              has_more: false
            })
          }
        } else {
          that.setData({
            listflag: false,
            has_more: false
          })
        }
        that.setData({
          showLoading: false
        })
        wx.stopPullDownRefresh();
      }, function(error) {
        console.log(error);
      });
  },
  changegrouplist: function(tabindex) {
    // console.log(this.data.all_group_list)
    switch (Number(tabindex)) {
      case 0:
        this.refreshgrouplist(config.get_group_buy_order)
        break; //全部
      case 1:
        this.refreshgrouplist(config.get_group_buy_unpaid_order)
        break; //待付款
      case 2:
        this.refreshgrouplist(config.get_group_buy_wait_order)
        break; //待成团
      case 3:
        this.refreshgrouplist(config.get_group_buy_success_order)
        break; //拼团成功
      case 4:
        this.refreshgrouplist(config.get_group_buy_fail_order)
        break; //拼团失败
    }
  },
  myorderdetail: function(e) { //redirectTo
    if (this.data.floor) {
      wx.redirectTo({
        url: '/pages/other/myorderdetail/myorderdetail?orderid=' + e.currentTarget.dataset.orderid,
      })
    } else {
      wx.navigateTo({
        url: '/pages/other/myorderdetail/myorderdetail?orderid=' + e.currentTarget.dataset.orderid,
      })
    }
  },
  tradepaying: function(e) {
    var that = this
    wx.login({
      success: function(res) {
        shihan.sendRequest(config.get_wx_perpay, 'POST', {
            trade_no: e.currentTarget.dataset.orderno,
            code: res.code
          })
          .then(function(response) {
            wx.requestPayment({
              "appId": response.data.data.sh_appid,
              'timeStamp': new String(response.data.data.sh_timestamp),
              'nonceStr': response.data.data.sh_noncestr,
              'package': response.data.data.sh_package,
              'signType': 'MD5',
              'paySign': response.data.data.sh_sign,
              'success': function(res) {
                if (that.data.partnerbuy == 1) {
                  app.data.partnerflag = 1
                  app.data.setupdownloadimgflag = 0
                  wx.switchTab({
                    url: '/pages/mine/mine',
                  })
                } else {
                  wx.redirectTo({
                    url: '/pages/paysuccess/paysuccess?tradeno=' + e.currentTarget.dataset.orderno,
                  })
                }
                that.refreshlist(that.data.currentTab)
              },
              'fail': function(res) {
                that.refreshlist()
              }
            })
          }, function(error) {
            console.log(error);
          });
      }
    })
  },
  refreshlist: function() {
    this.setData({
      page: 1,
      page_size: 20,
      listflag: true,
      has_more: true,
      showLoading: true,
      all_group_list: []
    });
    this.changegrouplist(this.data.currentTab)
  },
  onReachBottom: function() {
    if (that.data.listflag) {
      that.setData({
        listflag: false
      })
      this.changegrouplist(this.data.currentTab)
    }
  },
  pullgroup: function(e) { //喊人参团
    wx.navigateTo({
      url: '/pages/other/groupdetail/groupdetail?orderno=' + e.currentTarget.dataset.orderno,
    })
  },
  groupdetail: function(e) { //拼团详情  set_group_buy_cancel
    wx.navigateTo({
      url: '/pages/other/groupdetail/groupdetail?orderno=' + e.currentTarget.dataset.orderno,
    })
  },
  cancelgroup: function(e) { //取消订单
    var that = this
    var cueword = '取消拼团'
    var orderno = e.currentTarget.dataset.orderno
    wx.showModal({
      title: "提示",
      content: '确定要' + cueword + '吗？',
      confirmText: "确认",
      confirmColor: "#c40000",
      showCancel: true,
      success: function(res) {
        if (res.confirm) {
          that.orderoperate(config.set_group_buy_cancel, {
            order_no: orderno
          }, cueword)
        }
      }
    })
  },
  canceltrade: function(e) { //取消订单
    var that = this
    var cueword = '取消拼团'
    var orderno = e.currentTarget.dataset.orderno
    wx.showModal({
      title: "提示",
      content: '确定要' + cueword + '吗？',
      confirmText: "确认",
      confirmColor: "#c40000",
      showCancel: true,
      success: function(res) {
        if (res.confirm) {
          that.orderoperate(config.set_trade_cancel, {
            trade_no: orderno,
            reason: 6
          }, cueword)
        }
      }
    })
  },
  orderoperate: function(a, b, c) {
    var that = this
    shihan.sendRequest(a, 'POST', b)
      .then(function(response) {
        if (response.data.code == 0) {
          if (response.data.data) {
            that.setData({
              showLoading: true
            })
            shihan.showToast.call(that, c + '成功');
            that.refreshlist();
          } else {
            shihan.showToast.call(that, '网路出错了，请稍后再试');
          }
        } else {
          shihan.showToast.call(that, response.data.msg);
        }
      }, function(error) {
        console.log(error);
      });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  }
})